//
//  NetworkAutoCancelModel.swift
//  SwiftNetwork
//
//  Created by 万孟超 on 2022/1/12.
//

import UIKit

class NetworkAutoCancelModel: NSObject {
    
    lazy var pendingEngines : [NetworkEngine] = {
        print("pendingEngines懒加载初始化")
        return []
    }()
    
    func addEngine(engine : NetworkEngine) {
        pendingEngines.append(engine)
        print("请求自动关联")
    }
    
    func removeEngine(engine : NetworkEngine) {
        if let index = pendingEngines.firstIndex(where: {$0 === engine}) {
            pendingEngines.remove(at: index)
            print("请求关联移除")
        }
    }
    
    deinit {
        for engine in pendingEngines {
            engine.cancelNetwork()
            print("正在请求的自动取消")
        }
        pendingEngines.removeAll()
    }
}
